--
-- @Author:      
-- @DateTime:    2019-12-20 14:02:03
-- @Description: 用户对象
--  


local UserInfo = import("UserInfo")

local table_insert = table.insert
local table_remove = table.remove
local table_mix = table.mix

local UserManager = class("UserManager")

-- 初始化
function UserManager:ctor(config)
    self.config = config --配置
    self.user_list = {} --玩家列表

end

-- 重置数据
function UserManager:clear()
    for uid, v in pairs(self.user_list) do 
        self:removeUser(uid)
    end
    self.user_list = {} --玩家列表
end

-- 获取用户列表
function UserManager:getUserList()
	return self.user_list
end

--取玩家信息
function UserManager:getUser(uid)
    local user = self.user_list[uid]
    if not user then
        dbglog("getUser not find user  uid:",uid)
        return 
    end
    return user
end

-- 创建用户信息
function UserManager:createUser(uid)
     --先拉取玩家信息
    local ok, base_data = R().basesvr{key=uid}:call('.msg_handler','get_base_data',uid)
    if not ok then
        errlog(uid,'createUser failed to get base_data', uid)
        return false
    end
    -- dbglog("____玩家进桌base_data_____", base_data)
    -- 再拉取玩家的登入信息 跟大厅拉取
    local ok,enter_data = R().hallsvr{key=uid}:call('.msg_handler','get_enter_data',uid)
    if not ok then
        errlog(uid,'failed to get enter_data')
        -- enter_data = {}
        return false
    end
    -- dbglog("____玩家进桌enter_data_____",dump(enter_data))
    local user_info = UserInfo.new(base_data, enter_data)

    return user_info
end

--添加用户信息
function UserManager:addUser(uid)	
    --玩家已存在
    local user = self.user_list[uid]
    if user then    
        return user
    end  
    --拉取玩家信息
    local user = self:createUser(uid)
    if not user then
        return false, 1 --创建失败
    end
    -- dbglog("_____addUser:",uid)
    self.user_list[uid] = user
	return user
end

--移除用户信息
function UserManager:removeUser(uid)
	local user_info = self.user_list[uid]
    if not user_info then
		dbglog("_______removeUser_的玩家信息不存在____",user_info)
		return false
    end 	
    self.user_list[uid] = nil
    dbglog("__UserManager_user_id_删除_", uid )
	return true
end

--设置所有玩家的状态值
function UserManager:setAllUserValue(name, value)
    for uid, user in pairs(self.user_list) do 
        user[name] = value
    end
end

--玩家数
function UserManager:getUserNum()
    local num = 0
    for uid, user in pairs(self.user_list) do 
        num = num + 1
    end
    return num
end





---------------------------------------------------------------------------



---------------------------------------------------------------------------


return UserManager

